package com.hjm.project.agentPosOperation.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.hjm.common.utils.DateUtils;
import com.hjm.common.utils.StringUtils;
import com.hjm.common.utils.security.ShiroUtils;
import com.hjm.framework.config.CustomConfig;
import com.hjm.project.AgentMarketingReward.domain.AgentMarketingReward;
import com.hjm.project.AgentMarketingReward.service.IAgentMarketingRewardService;
import com.hjm.project.agentInfo.agentInfo.domain.AgentInfo;
import com.hjm.project.agentInfo.agentInfo.service.IAgentInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.hjm.framework.aspectj.lang.annotation.Log;
import com.hjm.framework.aspectj.lang.enums.BusinessType;
import com.hjm.project.agentPosOperation.domain.AgentPosOperation;
import com.hjm.project.agentPosOperation.service.IAgentPosOperationService;
import com.hjm.framework.web.controller.BaseController;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.common.utils.poi.ExcelUtil;
import com.hjm.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 机具采购、退款Controller
 * 
 * @author maker-platform
 * @date 2021-04-07
 */
@Controller
@RequestMapping("/agentPosOperation/agentPosOperation")
public class AgentPosOperationController extends BaseController
{
    private String prefix = "agentPosOperation";

    @Resource
    private IAgentPosOperationService agentPosOperationService;     //申请打款单
    @Resource
    private IAgentInfoService agentInfoService;                     //代理商
    @Resource
    private IAgentMarketingRewardService marketingRewardService;    //奖励配置

    @RequiresPermissions("agentPosOperation:agentPosOperation:view")
    @GetMapping()
    public String agentPosOperation()
    {
        return prefix + "/agentPosOperation";
    }


    /**
     * 前往审核页面
     * @Param []
     * @Return java.lang.String
     */
    @RequiresPermissions("auditAgentPosOperation:auditAgentPosOperation:view")
    @GetMapping("/auditAgentPosOperation")
    public String auditAgentPosOperation()
    {
        return prefix + "/auditAgentPosOperation";
    }


    /**
     * 前往所有代理页面(查找带回)
     * @Param []
     * @Return java.lang.String
     */
    @GetMapping("selectAgentInfoAllList")
    public String selectAgentInfoAllList()
    {
        return prefix + "/selectAgentInfoAllList";
    }


    /**
     * 获取所有配置奖励的代理
     * @Param []
     * @Return java.lang.String
     */
    @GetMapping("selectAgentInfoList")
    public String selectAgentInfoList(@RequestParam Map params, ModelMap mmp)
    {
        mmp.put("agentNum",params.get("agentNum"));
        return prefix + "/selectAgentInfoList";
    }


    /**
     * 获取所有代理信息
     * @Param []
     * @Return java.lang.String
     */
    @PostMapping("/selectAgentInfoListData")
    @ResponseBody
    public TableDataInfo selectAgentInfoListData(AgentInfo agentInfo)
    {
        startPage();
        List<AgentInfo> list = agentInfoService.selectAgentInfoList(agentInfo);
        return getDataTable(list);
    }


    /**
     * 获取配置过奖励的代理
     * @Param []
     * @Return java.lang.String
     */
    @PostMapping("/selectList")
    @ResponseBody
    public TableDataInfo selectList(AgentInfo agentInfo)
    {
        AgentMarketingReward marketingReward = new AgentMarketingReward();
        marketingReward.setTopAgentNum(agentInfo.getAgentNum());
        marketingReward.setStart(DateUtils.getDate());
        marketingReward.setStatus("1");
        startPage();
        List<AgentMarketingReward> list = marketingRewardService.selectAgentMarketingRewardList(marketingReward);
        return getDataTable(list);
    }




    /**
     * 查询机具采购、退款列表
     */
    @RequiresPermissions("agentPosOperation:agentPosOperation:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AgentPosOperation agentPosOperation)
    {
        startPage();
        List<AgentPosOperation> list = agentPosOperationService.selectAgentPosOperationList(agentPosOperation);
        return getDataTable(list);
    }

    /**
     * 导出机具采购、退款列表
     */
    @RequiresPermissions("agentPosOperation:agentPosOperation:export")
    @Log(title = "机具采购、退款", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AgentPosOperation agentPosOperation)
    {
        List<AgentPosOperation> list = agentPosOperationService.selectAgentPosOperationList(agentPosOperation);
        ExcelUtil<AgentPosOperation> util = new ExcelUtil<AgentPosOperation>(AgentPosOperation.class);
        return util.exportExcel(list, "agentPosOperation");
    }

    /**
     * 新增机具采购、退款
     */
    @GetMapping("/add")
    public String add(ModelMap mmp) {
        mmp.put("endDate",DateUtils.getYear(DateUtils.YYYY_MM_DD,1));
        return prefix + "/add";
    }

    /**
     * 新增保存机具采购、退款
     */
    @RequiresPermissions("agentPosOperation:agentPosOperation:add")
    @Log(title = "机具采购、退款", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("pzImg") MultipartFile pzImg, AgentPosOperation agentPosOperation) {

        //终端类型
        if (agentPosOperation.getPosType() == null) {
            return error("请选择终端类型");
        }
        //打款方式
        if (StringUtils.isEmpty(agentPosOperation.getDkWay())) {
            return error("请选择打款方式");
        }

        //打款类型
        if (StringUtils.isEmpty(agentPosOperation.getRewardType())) {
            return error("请选择打款类型");
        }
        return toAjax(agentPosOperationService.insertAgentPosOperation(pzImg,agentPosOperation));
    }

    /**
     * 修改机具采购、退款
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        AgentPosOperation agentPosOperation = agentPosOperationService.selectAgentPosOperationById(id);
        if (StringUtils.isNotEmpty(agentPosOperation.getImageUrl())) {
            agentPosOperation.setImageUrl(CustomConfig.getImgUrl() + agentPosOperation.getImageUrl());
        }
        mmap.put("agentPosOperation", agentPosOperation);
        return prefix + "/edit";
    }

    /**
     * 修改保存机具采购、退款
     */
    @RequiresPermissions("agentPosOperation:agentPosOperation:edit")
    @Log(title = "机具采购、退款", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AgentPosOperation agentPosOperation)
    {
        return toAjax(agentPosOperationService.updateAgentPosOperation(agentPosOperation));
    }


    /**
     * 修改保存机具采购、退款
     */
    @RequiresPermissions("agentPosOperation:agentPosOperation:edit")
    @Log(title = "审核", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ResponseBody
    public AjaxResult update(AgentPosOperation agentPosOperation) {

        //审核状态
        if (StringUtils.isEmpty(agentPosOperation.getApplyStatus())) {
            return error("请选择审核状态");
        }

        //拒绝请填写备注
        if (agentPosOperation.getApplyStatus().equals("2") && StringUtils.isEmpty(agentPosOperation.getNote())) {
            return error("请填写拒绝原因");
        }

        AgentPosOperation operation1 = agentPosOperationService.selectAgentPosOperationById(agentPosOperation.getId());
        if (operation1 != null && !operation1.getApplyStatus().equals("0")) {
            return error("当前审核单非待审核状态");
        }

        AgentPosOperation operation = new AgentPosOperation();
        operation.setExamineDate(new Date());
        operation.setExamineMan(ShiroUtils.getSysUser().getLoginName());
        operation.setApplyStatus(agentPosOperation.getApplyStatus());
        operation.setId(agentPosOperation.getId());
        operation.setNote(agentPosOperation.getNote());
        return toAjax(agentPosOperationService.updateAgentPosOperation(operation));
    }



    /**
     * 删除机具采购、退款
     */
    @RequiresPermissions("agentPosOperation:agentPosOperation:remove")
    @Log(title = "机具采购、退款", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(agentPosOperationService.deleteAgentPosOperationByIds(ids));
    }
}
